package com.tzu.ft.ui.comps;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

import com.tzu.ft.ui.utils.FTUIConst;
import com.tzu.ft.ui.utils.FTUITools;

/**
 * 文件进度框
 */
public class ProcessPanel extends JPanel {

    private static final long serialVersionUID = 1L;

    // 按钮图标
    public static final String TAG_RUN = "▶";
    public static final String TAG_STOP = "❚❚";
    public static final String TAG_DEL = "✖";

    /**
     * 面板宽度测试值
     */
    private static int WIDTH_TEST = 750;

    /**
     * 面板总高度
     */
    public static final int HEIGHT_ALL = 50;
    /**
     * 左侧组件单个高度
     */
    public static final int HEIGHT_ONE = 20;

    /**
     * 组件左右间距
     */
    public static final int WIDTH_SPACE = 5;
    /**
     * 组件上下间距
     */
    public static final int HEIGHT_SPACE = 3;
    /**
     * 组件上下间距
     */
    public static final int HEIGHT_SPACE_BTN = 5;

    /**
     * 按钮宽度
     */
    public static final int WIDTH_BTN = 50;
    /**
     * 按钮高度
     */
    public static final int HEIGHT_BTN = 40;

    /**
     * 进度条高度
     */
    public static final int HEIGHT_PROGRESS = 30;
    /**
     * 进度条宽度
     */
    public static final int WIDTH_PROGRESS = 160;

    /**
     * 速度显示框高度
     */
    public static final int HEIGHT_SPEED = HEIGHT_PROGRESS;
    /**
     * 速度显示框宽度
     */
    public static final int WIDTH_SPEED = 100;

    private int id;
    private String title;
    private String titleTip;
    private ProcessAction processAction;

    private JLabel labelFilename;
    private JLabel labelFiletype;
    private JLabel labelUsetime;

    private JLabel labelSpeed;
    private JProgressBar progressBar;
    private JButton btnA;
    // 删除按钮
    private JButton btnX;

    private int widthAll;

    private JPanel parentPanel;
    private ProcessPanel processPanel;

    // 运行状态：0未开始，1运行，2暂停
    private int status = 0;

    public ProcessPanel(int id, String title,String titleTip, JPanel parentPanel) {
        this.id = id;
        this.title = title;
        this.titleTip = titleTip;
        this.parentPanel = parentPanel;
        this.processPanel = this;
        this.widthAll = parentPanel.getWidth();
//		this.widthAll = WIDTH_TEST;
        setSize(widthAll, HEIGHT_ALL);
        setLayout(null);
        setBackground(FTUIConst.COLOR_DEF);
        this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        loadGroup(parentPanel);
    }

    /**
     * 设定文件类型
     */
    public void showFiletypeAndSize(String type, String size) {
        String show = "文件类型：" + type + "，文件大小：" + size;
        labelFiletype.setText(show);
        labelFiletype.setToolTipText(show);
    }

    /**
     * 显示速度
     *
     * @param normalInfo 正常信息true，不正常信息false
     */
    public void showSpeedAndInfo(String speed, boolean normalInfo) {
        labelSpeed.setText(speed);
        labelSpeed.setToolTipText(speed);
        if (normalInfo == false) {
            // 设置不透明
            setBackground(FTUIConst.COLOR_ERROR);
        }
    }

    public void showPrecent(double precent) {
        progressBar.setValue((int) precent);
    }

    public void showUsetime(String time) {
        String info = "已用时：" + time;
        labelUsetime.setText(info);
    }

    /**
     * 显示运行
     */
    public void showRun() {
        if (status != 1) {
            status = 1;
            btnA.setText(TAG_STOP);
            setBackground(FTUIConst.COLOR_RUN);
        }
    }

    /**
     * 显示停止
     */
    public void showStop() {
        if (status != 2) {
            status = 2;
            btnA.setText(TAG_RUN);
            labelSpeed.setText("已暂停");
            setBackground(FTUIConst.COLOR_STOP);
        }
    }

    /**
     * 加载组件
     */
    private void loadGroup(final JPanel parentPanel) {

        int labelwidtha = widthAll - WIDTH_PROGRESS - WIDTH_SPEED - WIDTH_BTN * 2 - WIDTH_SPACE * 6;
        // 文件名
        labelFilename = new JLabel(title);
        labelFilename.setLocation(WIDTH_SPACE, HEIGHT_SPACE);
        labelFilename.setSize(labelwidtha, HEIGHT_ONE);
        labelFilename.setToolTipText(titleTip);
        add(labelFilename);

        // 文件类型
        labelFiletype = new JLabel("文件类型：—— ——，文件大小：—— ——");
        labelFiletype.setLocation(WIDTH_SPACE, HEIGHT_SPACE + HEIGHT_ONE);
        labelFiletype.setSize((int) (labelwidtha * 0.65), HEIGHT_ONE);
        labelFiletype.setToolTipText(labelFiletype.getText());
        add(labelFiletype);

        // 以用时
        labelUsetime = new JLabel("已用时：0h 0m 0s");
        labelUsetime.setLocation(WIDTH_SPACE + (int) (labelwidtha * 0.65) + 10, HEIGHT_SPACE + HEIGHT_ONE);
        labelUsetime.setSize((int) (labelwidtha * 0.35) - 10, HEIGHT_ONE);
        add(labelUsetime);

        // 进度条
        int labelxa = labelwidtha + WIDTH_SPACE * 2;
        progressBar = new JProgressBar();
        progressBar.setLocation(labelxa, 10);
        progressBar.setSize(WIDTH_PROGRESS, HEIGHT_PROGRESS);
        progressBar.setStringPainted(true);
        progressBar.setFont(FTUITools.makeFont(progressBar.getFont(), Font.BOLD, 14));
        add(progressBar);
        progressBar.setValue(0);// 随机进度

        // 速度显示
        int labelxb = labelxa + WIDTH_PROGRESS + WIDTH_SPACE;
        labelSpeed = new JLabel("等待发送...");
        labelSpeed.setLocation(labelxb, HEIGHT_SPACE * 3);
        labelSpeed.setSize(WIDTH_SPEED, HEIGHT_PROGRESS);
        add(labelSpeed);

        // 开始暂停按钮
        int labelxc = labelxb + WIDTH_SPEED + WIDTH_SPACE;
        btnA = new JButton(TAG_STOP);
        btnA.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                if (processPanel.status == 0 || processPanel.status == 2) {
                    showRun();
                } else if (processPanel.status == 1) {
                    showStop();
                }
            }
        });
        btnA.setLocation(labelxc, HEIGHT_SPACE_BTN);
        btnA.setSize(WIDTH_BTN, HEIGHT_BTN);
        add(btnA);
        // 暂时不可见
        btnA.setVisible(false);

        // 删除按钮
        int labelxd = labelxc + WIDTH_BTN + WIDTH_SPACE;
        btnX = new JButton("");
        btnX.setIcon(new ImageIcon(ProcessPanel.class.getResource("/com/tzu/ft/ui/icons/delete_20px.png")));
        btnX.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    processAction.exit();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
                delete();
            }
        });
        btnX.setLocation(labelxd, HEIGHT_SPACE_BTN);
        btnX.setSize(WIDTH_BTN, HEIGHT_BTN);
        add(btnX);

    }

    public void delete() {
        processPanel.parentPanel.remove(processPanel);
        // 更新UI
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                SwingUtilities.updateComponentTreeUI(processPanel.parentPanel);
            }
        });

    }

    public void setProcessAction(ProcessAction processAction) {
        this.processAction = processAction;
    }

    /**
     * 处理事件
     */
    public static interface ProcessAction {
        void stop();

        void exit();
    }

    // =====================================================================

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

}
